package org.yangjie.config.security;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.yangjie.entity.Role;
import org.yangjie.entity.User;

/**
 * 用户认证详情实现
 * @author YangJie [2019年1月17日]
 */
@SuppressWarnings("serial")
public class UserDetailsImpl implements UserDetails{
	
	private User user;
	private List<Role> roleList;
	
	public UserDetailsImpl(User user){
		this.user = user;
	}
	
	public UserDetailsImpl(User user, List<Role> roleList){
		this.user = user;
		this.roleList = roleList;
	}

	
	@Override // 角色/权限列表
	public Collection<? extends GrantedAuthority> getAuthorities() {
		List<GrantedAuthority> authorities = new ArrayList<>();
        for(Role role : roleList) {
        	// 添加角色/权限时 需要添加固定前缀: ROLE_
            authorities.add(new SimpleGrantedAuthority("ROLE_" + role.getName()));
        }
        return authorities;
	}

	@Override
	public String getPassword() {
		return user.getPassword();
	}

	@Override
	public String getUsername() {
		return user.getUsername();
	}

	@Override // 帐号过期
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override // 帐号锁定
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override // 凭证过期
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override // 是否可用
	public boolean isEnabled() {
		return true;
	}
	
}
